package com.my.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.entity.Clazz;
import com.my.entity.ClazzPersonCount;
import com.my.entity.ClazzQueryParam;
import com.my.entity.PageBean;
import com.my.mapper.ClazzMapper;
import com.my.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageBean queryList(ClazzQueryParam param) throws Exception {
        PageBean pageBean = new PageBean();
        // 设置分页参数
        PageHelper.startPage(param.getPage(),param.getPageSize());

        // 1. 调用Mapper获取对象数据
        List<Clazz> clazzList = clazzMapper.queryList(param);

        // 设置课程状态 -new
        clazzList.forEach(clazz -> {
            if (clazz.getBeginDate().toEpochDay() > LocalDate.now().toEpochDay()) {
                //班级状态 - 未开班 , 在读 , 已结课
                clazz.setStatus("未开班");
            } else if (clazz.getEndDate().toEpochDay() < LocalDate.now().toEpochDay()) {
                clazz.setStatus("已结课");
            }else {
                clazz.setStatus("在读");
            }
        });

        Page p  = (Page) clazzList;
        pageBean.setTotal(p.getTotal());
        pageBean.setRows(clazzList);
        return pageBean;
    }


    /**
     * 新增员工
     * @param clazz
     */
    @Override
    public void addClazz(Clazz clazz) {
        // 1. 补充数据
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        log.info("server:{}", clazz);
        // 2. 调用mapper的方法
        clazzMapper.addClazz(clazz);
    }

    /**
     * 通过Id查找班级
     * @param id
     * @return
     */
    @Override
    public Clazz getClazzById(Integer id) {
        // 调用mapper的方法
        Clazz clazz = clazzMapper.getClazzById(id);
        log.info("server:{}", clazz);
        return clazz;
    }

    @Override
    public void updateClazz(Clazz clazz) {
        log.info("服务controller:{}",clazz);
        // 补充基本信息
        clazz.setUpdateTime(LocalDateTime.now());

        // 调用mapper的方法
        int row =  clazzMapper.updateClazz(clazz);
        log.info("受影响行:{}",row);
    }

    @Override
    public void deleteClazzById(Integer id) {
        // 调用mapper方法
        clazzMapper.deleteClazzById(id);
    }

    /**
     * 获得所有的班级信息
     * @return
     */
    @Override
    public List<Clazz> getAllClazzList() {
        // 调用Mapper
        List<Clazz> list= clazzMapper.getAllClazzList();
        return list;
    }

    /**
     * 班级人数统计
     * @return
     */
    @Override
    public ClazzPersonCount getStudentCountData() {

        // 收 班级列表
        List<String> clazzList = clazzMapper.getClazzName();
        log.info("clazzList:{}",clazzList);
        // 统计表
        List<Integer> clazzCount = clazzMapper.getClazzCount(clazzList);
        log.info("clazzCount:{}",clazzCount);
        ClazzPersonCount clazzPersonCount = new ClazzPersonCount();
        clazzPersonCount.setClazzList(clazzList);
        clazzPersonCount.setCountList(clazzCount);
        return clazzPersonCount;
    }

}
